package customerEnter.Frame.Aggregate.aggregatePlug.Execute;

import static com.google.common.collect.Maps.newHashMap;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import customerEnter.Frame.Aggregate.aggregatePlug.Intefase.AggregateUnionExecute;
import customerEnter.Frame.Aggregate.aggregatePlug.Intefase.AggregateUnionPlugIntefase;
import customerEnter.Frame.Aggregate.aggregatePlug.JDBC.JdbcTemateAggregateUnionPlug;
import customerEnter.Frame.Aggregate.aggregatePlug.JDBC.JdbcTemateTooAggregateUnionPlug;
import customerEnter.Frame.Aggregate.aggregatePlug.redis.RedisAggregateUnionPlug;
import customerEnter.Frame.Aggregate.aggregatePlug.redis.StringRedisAggregateUnionPlug;
import customerEnter.Frame.Aggregate.application.JDBC.JdbcTemateAggregateApplictationServer;
import customerEnter.Frame.Aggregate.application.JDBC.JdbcTemateToolAggregateApplictationServer;
import customerEnter.Frame.Aggregate.application.inteface.AggregateApplication;
import customerEnter.Frame.Aggregate.application.inteface.JdbcTemateAggregateApplication;
import customerEnter.Frame.Aggregate.application.inteface.JdbcTemateToolAggregateApplication;
import customerEnter.Frame.Aggregate.application.inteface.RedisTemateAggregateApplication;
import customerEnter.Frame.Aggregate.application.inteface.StringRedisTemateAggregateApplication;
import customerEnter.Frame.Aggregate.application.redis.RedisTemateAggregateApplictationServer;
import customerEnter.Frame.Aggregate.application.redis.StringRedisTemateAggregateApplictationServer;

/**
 * 
 * 
 * 类名称：AggregateUnionExecuteServer 类描述： 创建人：dy 创建时间：2017-4-21 下午3:19:30
 * 
 */
@SuppressWarnings("rawtypes")
public class AggregateUnionExecuteServer implements AggregateUnionExecute {

	private Set<AggregateUnionPlugIntefase>					aggregateUnionPlug;

	private Map<Class<?>, AggregateUnionExecuteMatching>	map	= newHashMap();

	public AggregateUnionExecuteServer() {
		map.put(JdbcTemateAggregateApplication.class, new AggregateUnionExecuteMatching(
				JdbcTemateAggregateUnionPlug.class, new JdbcTemateAggregateApplictationServer()));

		map.put(JdbcTemateToolAggregateApplication.class, new AggregateUnionExecuteMatching(
				JdbcTemateTooAggregateUnionPlug.class, new JdbcTemateToolAggregateApplictationServer()));

		map.put(RedisTemateAggregateApplication.class, new AggregateUnionExecuteMatching(RedisAggregateUnionPlug.class,
				new RedisTemateAggregateApplictationServer()));

		map.put(StringRedisTemateAggregateApplication.class, new AggregateUnionExecuteMatching(
				StringRedisAggregateUnionPlug.class, new StringRedisTemateAggregateApplictationServer()));

	}

	public Set<AggregateUnionPlugIntefase> getAggregateUnionPlug() {

		return aggregateUnionPlug;
	}

	public void setAggregateUnionPlug(Set<AggregateUnionPlugIntefase> aggregateUnionPlug) {
		this.aggregateUnionPlug = aggregateUnionPlug;
	}

	/**
	 * 把數據源 放入應用
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends AggregateApplication> T execute(Class<T> class1) {
		Iterator<AggregateUnionPlugIntefase> it = aggregateUnionPlug.iterator();
		AggregateApplication<T> aggregateApplication = null;
		AggregateUnionExecuteMatching aggregateUnionExecuteMatching = map.get(class1);
		while (it.hasNext()) {
			AggregateUnionPlugIntefase aggregateUnionPlugIntefase = it.next();
			if (aggregateUnionExecuteMatching.getAggregateUnionPlugIntefase() == aggregateUnionPlugIntefase.getClass()) {
				aggregateApplication = aggregateUnionExecuteMatching.getAggregateApplication();
				aggregateApplication.InitAggregateUnionPlug(aggregateUnionPlugIntefase);
				break;
			}
		}
		return (T) aggregateApplication;
	}

}
